﻿using System;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using StockholmsStad.Runtime.Serialization.DataContract;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Models
{
    public class TemplatesPresentationModel : INotifyPropertyChanged
    {
        public event EventHandler<DataEventArgs<TemplatesPresentationModel>> Saved;
        public event EventHandler<DataEventArgs<TemplatesPresentationModel>> Deleted;
        public event EventHandler<DataEventArgs<TemplatesPresentationModel>> Added;
        public DelegateCommand<object> SaveCommand { get; private set; }
        public DelegateCommand<object> DeleteCommand { get; private set; }
        public DelegateCommand<object> AddCommand { get; private set; }

        public TemplatesPresentationModel()
        {
            AddCommand = new DelegateCommand<object>(Add);
            SaveCommand = new DelegateCommand<object>(Save);
            DeleteCommand = new DelegateCommand<object>(Delete);

            PropertyChanged += OnPropertyChangedEvent;
        }

        #region Properties

        public string HeaderInfo
        {
            get { return HeaderNames.Templates; }
        }

        public bool ShouldSave { get; set; }

        private int? marriageCertificateId;
        public int? MarriageCertificateId
        {
            get
            {
                return marriageCertificateId;
            }
            set
            {
                if (!value.Equals(marriageCertificateId))
                {
                    marriageCertificateId = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("MarriageCertificateId"));
                }
            }
        }

        private int? reportDefinitionId;
        public int? ReportDefinitionId
        {
            get
            {
                return reportDefinitionId;
            }
            set
            {
                if (!value.Equals(reportDefinitionId))
                {
                    reportDefinitionId = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("ReportDefinitionId"));
                }
            }
        }

        private MarriageCertificateType selectedMarriageCertificateType;
        public MarriageCertificateType SelectedMarriageCertificateType
        {
            get
            {
                return selectedMarriageCertificateType;
            }
            set
            {
                if (!value.Equals(selectedMarriageCertificateType))
                {
                    selectedMarriageCertificateType = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectedMarriageCertificateType"));
                }
            }
        }

        private ValueIdPair selectedLanguage;
        public ValueIdPair SelectedLanguage
        {
            get
            {
                return selectedLanguage;
            }
            set
            {
                if (!value.Equals(selectedLanguage))
                {
                    selectedLanguage = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectedLanguage"));
                }
            }
        }

        private string heading;
        public string Heading
        {
            get
            {
                return heading;
            }
            set
            {
                if (!value.Equals(heading))
                {
                    heading = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Heading"));
                }
            }
        }

        private string inception;
        public string Inception
        {
            get
            {
                return inception;
            }
            set
            {
                if (!value.Equals(inception))
                {
                    inception = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Inception"));
                }
            }
        }

        private string betweenNames;
        public string BetweenNames
        {
            get
            {
                return betweenNames;
            }
            set
            {
                if (!value.Equals(betweenNames))
                {
                    betweenNames = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("BetweenNames"));
                }
            }
        }

        private string aboveDate;
        public string AboveDate
        {
            get
            {
                return aboveDate;
            }
            set
            {
                if (!value.Equals(aboveDate))
                {
                    aboveDate = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("AboveDate"));
                }
            }
        }

        private string city;
        public string City
        {
            get
            {
                return city;
            }
            set
            {
                if (!value.Equals(city))
                {
                    city = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("City"));
                }
            }
        }

        private string belowDate;
        public string BelowDate
        {
            get
            {
                return belowDate;
            }
            set
            {
                if (!value.Equals(belowDate))
                {
                    belowDate = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("BelowDate"));
                }
            }
        }

        private string signLine;
        public string SignLine
        {
            get
            {
                return signLine;
            }
            set
            {
                if (!value.Equals(signLine))
                {
                    signLine = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("SignLine"));
                }
            }
        }

        private string belowSignature;
        public string BelowSignature
        {
            get
            {
                return belowSignature;
            }
            set
            {
                if (!value.Equals(belowSignature))
                {
                    belowSignature = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("BelowSignature"));
                }
            }
        }

        #endregion

        private void OnPropertyChangedEvent(object sender, PropertyChangedEventArgs e)
        {
            SaveCommand.RaiseCanExecuteChanged();
        }

        private void Save(object obj)
        {
            OnSaved(new DataEventArgs<TemplatesPresentationModel>(this));
        }

        private void Delete(object obj)
        {
            OnDeleted(new DataEventArgs<TemplatesPresentationModel>(this));
        }

        private void Add(object obj)
        {
            OnAdded(new DataEventArgs<TemplatesPresentationModel>(this));
        }

        private void OnSaved(DataEventArgs<TemplatesPresentationModel> e)
        {
            var savedHandler = Saved;

            if (savedHandler != null)
                savedHandler(this, e);
        }

        private void OnDeleted(DataEventArgs<TemplatesPresentationModel> e)
        {
            var deletedHandler = Deleted;

            if (deletedHandler != null)
                deletedHandler(this, e);
        }

        private void OnAdded(DataEventArgs<TemplatesPresentationModel> e)
        {
            var addedHandler = Added;

            if (addedHandler != null)
                addedHandler(this, e);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
